package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PApply;
import com.wuliaogame.entity.PPm;
import com.wuliaogame.entity.PUser;
import com.wuliaogame.mapper.PPlayerMapper;
import com.wuliaogame.mapper.PPmMapper;
import com.wuliaogame.mapper.PUserMapper;
import com.wuliaogame.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-21
 */
@RestController
@RequestMapping("/p-pm")
public class PPmController extends JSONAuthentication {
    @Autowired
    PPmMapper pPlayerMapper;
    @Autowired
    PUserMapper pUserMapper;
    @ApiOperation(value = "添加竞价申请")
    @PostMapping("/add")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestBody PPm pPm) throws Exception {
        PUser pUser = pUserMapper.selectById(pPm.getCjId());
        if(pUser.getClub()==null){
            Result ok = Result.ok().message("无俱乐部，请加入俱乐部再进行出价");
            this.WriteJSON(request, response, ok);
        }else{
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("cj_id",pPm.getCjId());
            wrapper.eq("jp_id",pPm.getJpId());
            if(pPlayerMapper.selectOne(wrapper)!=null){
                Result error = Result.error().message("已进行出价，请修改价格");
                this.WriteJSON(request, response, error);
            }else{
                Date date=new Date();
                Timestamp timestamp=new Timestamp(date.getTime());
                pPm.setCreatTime(timestamp);
                if(pPlayerMapper.insert(pPm)>0){
                    Result ok = Result.ok().message("参与竞价成功成功");
                    this.WriteJSON(request, response, ok);
                }else{
                    Result error = Result.error().message("参与竞价失败");
                    this.WriteJSON(request, response, error);
                }
            }
        }
    }

    @ApiOperation(value = "出价者修改自己的出价价格")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("price")int  price) throws Exception {
        PPm pPm = pPlayerMapper.selectById(id);
        pPm.setPrice(price);
        if (pPlayerMapper.updateById(pPm) > 0) {
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "被出价者修改状态(同意加入俱乐部)")
    @PostMapping("/updStatus")
    public void updStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        PPm pPm = pPlayerMapper.selectById(id);
        pPm.setStatus("同意");
        PUser chujiazhe = pUserMapper.selectById(pPm.getCjId());
        PUser beijingpaizhe = pUserMapper.selectById(pPm.getJpId());
        beijingpaizhe.setClub(chujiazhe.getClub());
        if (pPlayerMapper.updateById(pPm) > 0&&pUserMapper.updateById(beijingpaizhe)>0) {
            Result ok = Result.ok().message("成功加入俱乐部");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找全部竞价")
    @PostMapping("/findAll")
    public void findBynamea(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<PPm> page = new Page<>(current, size);
        Page<PPm> pages = pPlayerMapper.selectPage(page, null);
        List<PPm> list = pages.getRecords();
        for (PPm a: list){
            a.setCjUser(pUserMapper.selectById(a.getCjId()));
            a.setJpUser(pUserMapper.selectById(a.getJpId()));
        }
        int total = pPlayerMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("jinjia", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有竞价");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部通过出价人id")
    @PostMapping("/findAllByCjId")
    public void findAllByCjId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("cjrId")int cjrId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("cj_id",cjrId);
        Page<PPm> page = new Page<>(current, size);
        Page<PPm> pages = pPlayerMapper.selectPage(page, wrapper);
        List<PPm> list = pages.getRecords();
        for (PPm a: list){
            a.setCjUser(pUserMapper.selectById(a.getCjId()));
            a.setJpUser(pUserMapper.selectById(a.getJpId()));
        }
        int total = pPlayerMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("jinjia", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有竞价记录");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部通过被被竞拍选手id")
    @PostMapping("/findAllByXsId")
    public void findAllByXsId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("xsId")int xsId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("jp_id",xsId);
        Page<PPm> page = new Page<>(current, size);
        Page<PPm> pages = pPlayerMapper.selectPage(page, wrapper);
        List<PPm> list = pages.getRecords();
        for (PPm a: list){
            a.setCjUser(pUserMapper.selectById(a.getCjId()));
            a.setJpUser(pUserMapper.selectById(a.getJpId()));
        }
        int total = pPlayerMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("jinjia", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有竞价记录");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部通过被被竞拍选手id查出竞拍最高价")
    @PostMapping("/findAllByXsIdZgj")
    public void findAllByXsIdZgj(HttpServletRequest request, HttpServletResponse response,@RequestParam("xsId")int xsId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("jp_id",xsId);
        wrapper.orderByDesc("price");
        wrapper.last("limit 1");
       PPm pPm=pPlayerMapper.selectOne(wrapper);
        try {
            pPm.setCjUser(pUserMapper.selectById(pPm.getCjId()));
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            pPm.setJpUser(pUserMapper.selectById(pPm.getJpId()));
        }catch (Exception e){
            e.printStackTrace();
        }

        if (pPm!=null) {
            Result ok = Result.ok().data("jinjia", pPm);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有竞价记录");
        this.WriteJSON(request, response, error);
    }

}

